define([ "modules/wydatki" ], function(wydatki) {
	
	return {
		createGrid : function(config) {
			require([ "dojox/grid/DataGrid", "dojo/_base/xhr", "dojo/domReady!", "dojo/data/ItemFileWriteStore" ], function(DataGrid, xhr, ifws) {
				var grid = new DataGrid({
					id: config.gridId,
					height: '450px',
					query : {
						id : "*"
					},
					queryOptions : {},
					structure : [{
						name : "Data",
						field : "data",
						width : "10%"
					}, {
						name : "Nazwa",
						field : "nazwa",
						width : "70%"
					}, {
						name : "Kwota",
						field : "kwota",
						width : "20%"
					}],
					reloadStore: function() {
						xhr.get({
							url : config.url,
							handleAs : "json"
						}).then(function(data2) {
							   listStore = new dojo.data.ItemFileWriteStore({
							        data: {
							            items: data2
							        }
							    });
							   grid.setStore(listStore);
						});
					},
					setUrl : function(url) {
						config.url = url;
					}
				}, config.gridDiv);

				grid.reloadStore();
				grid.startup();
			});
		},
		createToolbar : function(config) {
			require([ "dojo/ready", "dijit/Toolbar", "dijit/form/Button",
			          "dojo/_base/xhr", "dijit/Dialog", "dojo/dom",
			          "dijit/registry", 'modules/ep'], function(ready, Toolbar, Button, xhr, Dialog, dom, registry, ep) {
					var toolbar = new Toolbar({}, config.divId);					
					
					var addButton = new Button({
						label : 'Nowy',
						showLabel : true,
						iconClass : "dijitIconNewTask",
						onClick : function() {
							ep.createFormWindow({
								id: 'addWydatekWindow',
								formId: 'wydatekForm',
								title: 'Dodaj nowy wydatek',
								url: 'wydatek!add.action',
								onSuccess: function(response) {
								    var gridNode = dom.byId(config.gridId);
								    var grid = registry.byNode(gridNode);
								    grid.reloadStore();
									
								    ep.showMsg(response);
								}
							})
						}
					});
					
					var editButton = new Button({
						label : 'Edytuj',
						showLabel : true,
						iconClass : "dijitIconEdit",
						onClick : function() {
							    var gridNode = dom.byId(config.gridId);
							    var grid = registry.byNode(gridNode);
							    var selectedItems = grid.selection.getSelected();
							    
								ep.createFormWindow({
									id: 'editWydatekWindow',
									formId: 'wydatekForm',
									title: 'Edytuj wydatek',
									url: 'wydatek!edit.action?wydatekId=' + selectedItems[0].id,
									onSuccess: function(resp) {
									    grid.reloadStore();
									    ep.showMsg(resp);
									}
								});
						}
					});
					
					var deleteButton = new Button({
						label : 'Usuń',
						showLabel : true,
						iconClass : "dijitIconDelete",
						onClick : function() {
							
							ep.createActionDialog({
								id: "confirmWindowWydatekDelete",
								title: "Pytanie",
								content: "Czy na pewno chcesz usunąć wybrany wydatek ?",
								beforeSubmit: function() {},
								onSuccess: function() {
									var gridDiv = dom.byId(config.gridId);
									var grid = registry.byNode(gridDiv);
									var selectedItems = grid.selection.getSelected();
									
									ep.requestGet("wydatek!delete.action?wydatekId=" + selectedItems[0].id, function(response) {
										ep.showMsg(response);
										grid.reloadStore();
									});
								}
							}) 
						}
					});
					
					toolbar.addChild(addButton);
					toolbar.addChild(editButton);
					toolbar.addChild(deleteButton);
				});
		}
	}
	
});